#include <stdio.h>
#include <string.h>
#include <arpa/inet.h>
#include <netinet/ip.h>
#include <stdint.h>
#include <stdlib.h>
#include "p.h"

#define INTERNAL_IP_PREFIX_1 "192.168."
#define INTERNAL_IP_PREFIX_2 "10."
#define INTERNAL_IP_PREFIX_3 "172.16."

//以太网帧处理函数
void process_ethernet(const uint8_t *packet,int len,TreeNode *next,void *arg){
    EthernetHeader *eth_header = (EthernetHeader *)packet;
    printf("Source MAC: %02x:%02x:%02x:%02x:%02x:%02x\n",
            eth_header->dst_mac[0],eth_header->dst_mac[1],eth_header->dst_mac[2],
            eth_header->dst_mac[3],eth_header->dst_mac[4],eth_header->dst_mac[5]);
    printf("Destination MAC: %02x:%02x:%02x:%02x:%02x:%02x\n",
            eth_header->dst_mac[0], eth_header->dst_mac[1], eth_header->dst_mac[2],
            eth_header->dst_mac[3], eth_header->dst_mac[4], eth_header->dst_mac[5]);
    if(next != NULL){
        next->process(packet + sizeof(EthernetHeader),len - sizeof(EthernetHeader),next->left,arg);
    }
}

//IP头处理函数
void process_ip(const uint8_t *packet,int len,TreeNode *next,void *arg){
    IPHeader *ip_header = (IPHeader *)packet;
    char src_ip_str[16];
    char dst_ip_str[16];
    inet_ntop(AF_INET,&(ip_header->src_ip),src_ip_str,INET_ADDRSTRLEN);
    inet_ntop(AF_INET,&(ip_header->dst_ip),dst_ip_str,INET_ADDRSTRLEN);

    printf("Source IP: %s\n",dst_ip_str);
    printf("Destination IP: %s\n",dst_ip_str);

    TrafficStats *stats = (TrafficStats *)arg;
    PacketFlags flags = {0};

    if (!strstr(src_ip_str, INTERNAL_IP_PREFIX_1) &&
    !strstr(src_ip_str, INTERNAL_IP_PREFIX_2) &&
    !strstr(src_ip_str, INTERNAL_IP_PREFIX_3)) {
    flags.is_external = 1;  // 若为外网 IP 地址，设置标志位
} else if (!strstr(dst_ip_str, INTERNAL_IP_PREFIX_1) &&
           !strstr(dst_ip_str, INTERNAL_IP_PREFIX_2) &&
           !strstr(dst_ip_str, INTERNAL_IP_PREFIX_3)) {
    flags.is_external = 1;  // 若目的 IP 地址为外网 IP 地址，设置标志位
}

    if(flags.is_external){
        if (strstr(src_ip_str,INTERNAL_IP_PREFIX_1) ||
            strstr(src_ip_str,INTERNAL_IP_PREFIX_2) ||
            strstr(src_ip_str,INTERNAL_IP_PREFIX_3)) {
            int found = 0;
            for(int i = 0;i < stats->count;i++){
                if(strcmp(stats->ips[i],src_ip_str) == 0) {
                    stats->traffic[i] += len;
                    found = 1;
                    break;
                }
            }
            if (!found && stats->count < 100) {
                strcpy(stats->ips[stats->count], src_ip_str);
                stats->traffic[stats->count] = len;
                stats->count++;
            }
           
    
        }


}